/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBL;

import java.sql.*;
import java.util.*;
import MDL.*;

/**
 *
 * @author Niklas
 */
public class DBOrdre implements IFDBOrdre
{

    private Connection con = DBConnection.getInstance().getDBcon();
    private ArrayList<Delordre> delOrdreList;

    public DBOrdre()
    {
        delOrdreList = new ArrayList<Delordre>();
    }

    public Ordre createOrdre(Ordre ord)
    {
        int ordreId = GetMax.getNextID("SELECT MAX(ordreId) FROM Ordre");
        ordreId++;
        String dato = "2010-01-01 12:01:00";

        String query = "INSERT INTO Ordre(ordreId, dato, kunde, pris) VALUES(" +
                ordreId + ",'" +
                dato + "','" +
                ord.getKunde() + "'," +
                ord.getPris() + ")";

        System.out.println(query);
        try
        {
            Statement state = con.createStatement();
            state.setQueryTimeout(5);
            state.executeQuery(query);
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR:" + e);
        }

        ord.setOrdreId(ordreId);
        return ord;
    }

    public void createDelordre(Delordre del)
    {
        String query = "INSERT INTO DelOrder(vareId, ordreId, antal, lagerId) VALUES(" +
                del.getVareId() + ", " +
                del.getOrdreId() + "," +
                del.getAntal() + "," +
                del.getLagerId() + ")";

        System.out.println(query);
        try
        {
            Statement state = con.createStatement();
            state.setQueryTimeout(5);
            state.executeQuery(query);
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR:" + e);
        }
    }

    public void updateProdukt(Produkt pro)
    {
        String query = "UPDATE Produkt SET samletAntal = " + pro.getSamletAntal() + " WHERE id = " + pro.getVareId();
        try
        {
            Statement state = con.createStatement();
            state.executeUpdate(query);
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR: " + e);
        }
    }

    public void deleteDelordre(int ordreId)
    {
        String query = "DELETE FROM delordre WHERE ordreId = " + ordreId;
        System.out.println("Delete ordre: " + query);
        try
        {
            Statement state = con.createStatement();
            state.executeUpdate(query);
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR: " + e);
        }
    }

    public void deleteOrdre(int ordreId)
    {
        String query = "DELETE FROM Ordre WHERE ordreId = " + ordreId;
        System.out.println("Delete Ordre: " + query);
        try
        {
            Statement state = con.createStatement();
            state.executeUpdate(query);
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR: " + e);
        }
    }

    public void finishSalg(float totalPrice, int ordreId)
    {
        String query = "UPDATE Ordre SET pris = " + totalPrice + " WHERE ordreId = " + ordreId;

        try
        {
            Statement state = con.createStatement();
            state.executeUpdate(query);
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR: " + e);
        }
    }

    /**
     * Den her finder de delorder der er knyttet til det salg vi er igang. Så putter den produkt_id og amount i et delorder
     * objekt og ligger det i et array og sender det til metoden putback() som så updater tabellen Product.
     * @param saleID
     */
    public void putBackStock(int ordreId)
    {
        String query = "SELECT vareId, antal FROM Delordre WHERE ordreId = " + ordreId;
        ResultSet results;
        Delordre del  = null;
        System.out.println("Find Delordrer: " +query);
        try
        {
            Statement state = con.createStatement();
            state.setQueryTimeout(5);
            results = state.executeQuery(query);

            while (results.next())
            {
                del = new Delordre();
                del.setVareId(results.getInt(1));
                del.setAntal(results.getInt(2));
                del.setLagerId(results.getInt(3));

                delOrdreList.add(del);
            }
            state.close();
            putback(delOrdreList);
        } catch (Exception e)
        {
            System.out.println("ERROR: " + e);
        }
    }

    public void putback(ArrayList<Delordre> doList)
    {
        int count = 0;
        while (count < doList.size())
        {
            try
            {
                Statement state = con.createStatement();
                state.setQueryTimeout(5);
                String squery = "UPDATE Produkt SET samletAntal = (samletAntal +" + doList.get(count).getAntal() + ") WHERE vareId = " + doList.get(count).getVareId();
                System.out.println("Update Produkter: " +squery);
                state.executeQuery(squery);
                state.close();

            } catch (Exception e)
            {
                System.out.println("ERROR: " + e);
            }
            count++;
        }
    }

    public Ordre findOrdre(int ordreId, Ordre ord)
    {

        int ordId = 0;
        ResultSet results;

        String query = "SELECT ordreId FROM Ordre WHERE ordreId ='" + ordreId + "'";
        try
        {
            Statement state = con.createStatement();
            results = state.executeQuery(query);
            if (results.next())
            {
                ord.setOrdreId(results.getInt(1));
                ord.setPris(results.getFloat(2));
            }
            state.close();
        } catch (Exception e)
        {
            System.out.println("ERROR: " + e);
        }
        return ord;
    }
}
